package Game;

import java.awt.Color;
import java.awt.Desktop;
import java.awt.Font;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.font.TextAttribute;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Map;

import javax.swing.Icon;
import javax.swing.JLabel;;

public class LinkedLabel extends JLabel {

	/**
	 * 
	 */
	private static final long serialVersionUID = -1116089825143616307L;
	
	
	public LinkedLabel(String caption, String link) {
		super(caption);
		init(link);
	}
	
	public LinkedLabel(Icon caption, String link) {
		super(caption);
		init(link);
	}
	
	private void init(String link) {
		this.setForeground(new Color(0x000099));
		this.setToolTipText(link);
		this.addMouseListener(new LinkPressed(link));
	}
	
	private class LinkPressed implements MouseListener {
		
		private URI link;
		
		private LinkPressed(String link) {
			try {
				this.link = new URI(link);
			} catch (URISyntaxException e) {
				e.printStackTrace();
			}
		}

		public void mouseClicked(MouseEvent e) {
			try {
				Desktop.getDesktop().browse(link);
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}

		@SuppressWarnings("unchecked")
		public void mouseEntered(MouseEvent e) {
			Font font = LinkedLabel.this.getFont();
			@SuppressWarnings("rawtypes")
			Map attributes = font.getAttributes();
			attributes.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
			LinkedLabel.this.setFont(font.deriveFont(attributes));
			repaint();
		}
		
		@SuppressWarnings("unchecked")
		public void mouseExited(MouseEvent e) {
			Font font = LinkedLabel.this.getFont();
			@SuppressWarnings("rawtypes")
			Map attributes = font.getAttributes();
			attributes.put(TextAttribute.UNDERLINE, null);
			LinkedLabel.this.setFont(font.deriveFont(attributes));
			repaint();
		}

		public void mousePressed(MouseEvent e) {
		}

		public void mouseReleased(MouseEvent e) {
		}
		
	}

}
